﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CMS.Data;
using CMS.Service.UnitOfWork;
using System.Data;

namespace CMS.Service.Repository
{
    public class WorkRepository : RepositoryBase<Work>
    {
        public WorkRepository() : base() { }

        public WorkRepository(WorkUnit unit) : base(unit) { }

        public override int CreateOrUpdate(Work entity)
        {
            try
            {
                Context.Entry(entity).State = entity.Id == 0 ? EntityState.Added : EntityState.Modified;
                Context.SaveChanges();
                return entity.Id;
            }
            catch { return 0; }
        }

        public override string Delete(int entityId)
        {
            try
            {
                var entity = Context.Works.SingleOrDefault(o => o.Id == entityId);
                Context.Works.Remove(entity);
                Context.SaveChanges();
                return "success";
            }
            catch { return "error"; }
        }

        public override Work GetById(int entityId)
        {
            try
            {
                return Context.Works.SingleOrDefault(p => p.Id == entityId);
            }
            catch { return null; }
        }

        public override List<Work> List()
        {
            try
            {
                return Context.Works.ToList();
            }
            catch { return null; }
        }

        public override List<Work> GetTableListById(int entityId)
        {
            throw new NotImplementedException();
        }

        public List<Work> List(string keys, int customer_id, int staff_id, DateTime assign_on_from, DateTime assign_on_to, int status, int orderby)
        {
            try
            {
                var query = Context.Works.Where(x => 1 == 1);
                if (!string.IsNullOrEmpty(keys))
                {
                    query = query.Where(x =>
                        x.BillNum.Contains(keys) ||
                        x.Content.Contains(keys) ||
                        x.Note.Contains(keys));
                }
                if (customer_id > 0)
                {
                    query = query.Where(x => x.AssignCustomer == customer_id);
                }
                if (staff_id > 0)
                {
                    query = query.Where(x => x.AssignStaff == staff_id);
                }
                if (assign_on_from > DateTime.MinValue)
                {
                    query = query.Where(x => x.AssignOn >= assign_on_from);
                }
                if (assign_on_to > DateTime.MinValue)
                {
                    query = query.Where(x => x.AssignOn <= assign_on_to);
                }
                if (status > 0)
                {
                    query = query.Where(x => x.Status == status);
                }
                switch (orderby)
                {
                    case 1:
                        query = query.OrderBy(x => x.Status);
                        break;
                    case 2:
                        query = query.OrderBy(x => x.AssignOn);
                        break;
                    case 3:
                        query = query.OrderBy(x => x.AssignStaff);
                        break;
                    case 4:
                        query = query.OrderBy(x => x.AssignCustomer);
                        break;
                    default:
                        query = query.OrderBy(x => x.CreatedOn);
                        break;
                }

                return query.ToList();
            }
            catch { return null; }
        }
    }
}
